<?php

namespace Matrix\Controller;

use Think\Model;
class AutoController extends CommonController {
	public function __construct() {
		parent::__construct ();
		parent::__config ( array (
				'model_name' => 'Auto' 
		) );
	}
	public function index() {
		parent::__config ( array (
				'fuzzy' => '' 
		) );
		
		$this->get_index_data();
		
		$table = I ( 'table' );
		if (! $table == null && ! empty ( $table )) {
			parent::__config ( array (
					'display' => 'table',
					'display_parms' => array (
							'table' => 1 
					) 
			) );
		}
		parent::index ();
	}
	
	//自动检测过期订单
	function batch_order(){
		$orderModel = D("Order");
		$where_order['order_statue']= array('in','0,1');
		$time_24 = strtotime("-1 day");
		$where_order['add_time']=array('lt',$time_24);
		$order_list = $orderModel->where($where_order)->select();
		foreach ($order_list as $key => $value) {
			$data_order['id']=$value['id'];
			$data_order['order_statue']=3;
			$orderModel->save($data_order);
		}
		echo "<script>alert('" . I ( 'username' ) . "检测完成！');</script>";
		$table = I ( 'table' );
		if (! $table == null && ! empty ( $table )) {
			parent::__config ( array (
					'display' => 'table',
					'display_parms' => array (
							'table' => 1
					)
			) );
		}
		
		
		$this->get_index_data();
		$this->display("index");
	}
	
	
	//统计用户月报表
	function user_monthly_balance(){
			$where['code_key']=date('Ym',strtotime(" -1 month "));
			$is_can = D("UserReport")->where($where)->select();
			if(!$is_can){
			$b_e_time = get_last_month_b_e_time();
			//获取上个月所有审核通过订单
			$where_order['balance_status']=1;//未结算
			$where_order['order_statue']=2;//审核通过
			$where_order['taocan_num']=array('neq',6);//审核通过
			$where_order['review_time']=array('between',array($b_e_time['b_time'],$b_e_time['e_time']));
			$orderModel = D("Order");
			$order_list = $orderModel->where($where_order)->select();
			$user_ids = get_user_id_from_array($order_list,'user_id');
	// 		dump($user_ids);
	// 		dump($order_list);
	// 		die();
			foreach ($user_ids as $key_user => $value_user) {
				$temp_money = 0;
				foreach ($order_list as $key_order => $value_order) {
					if($value_order['user_id']==$value_user){//相同用户数据
						$temp_money=$temp_money+$value_order['money'];
					}
				}
				$agent_info = get_agent_id_by_user_id($value_user);
				
				$dataList[]=array('user_id'=>$value_user,'agent_id'=>$agent_info['agent_id'],'date'=>date('Y')."年".date('m',strtotime(" -1 month "))."月",'consume'=>$temp_money,'created'=>time(),'modfied'=>time(),'deleted'=>1,'code_key'=>date('Ym',strtotime(" -1 month ")));
			}
			//开启事务
			$model = new Model();
			$model->startTrans();
			//批量写入
			$add_result = D('UserReport')->addAll($dataList);
			//修改订单结算状态
			$save_temp = true;
			foreach ($order_list as $k_order => $v_order) {
				$data_order['id']=$v_order['id'];
				$data_order['balance_status']=2;
				$save_result = $orderModel->save($data_order);
				if($save_result===false){
					$save_temp=false;
				}
			}
			if($add_result&&$save_temp){
				$model->commit();
				echo "<script>alert('统计完成！');</script>";
				
			}else{
				$model->rollback();
				echo "<script>alert('统计失败！');</script>";
			}
		
		}else{
			echo "<script>alert('上个月已统计过！');</script>";
		}
		$this->get_index_data();
		$this->display("index");
	}
	
	function agent_monthly_balance(){
		$is_in_day = is_in_day(1,2);
// 		if(!$is_in_day){
// 			$this->system_hint("请于1到2号再操作!", 100);
// 			$this->redirect('index');
// 		}
		
		$reportModel = D("Report");
		$where['code_key']=date('Ym',strtotime(" -1 month "));
		$is_can = $reportModel->where($where)->select();
		if(!$is_can){
				//获取上个月所有用户月报表
				$where['code_key']=date('Ym',strtotime(" -1 month "));
				$report_list = D("UserReport")->where($where)->select();
				$agent_ids = get_user_id_from_array($report_list,'agent_id');
				foreach ($agent_ids as $key_agent => $value_agent) {
					$temp_money = 0;//当月总消费
					foreach ($report_list as $key_report => $value_report) {
						if($value_report['agent_id']==$value_agent){//相同用户数据
							$temp_money=$temp_money+$value_report['consume'];
						}
					}
					$rate = get_rate($temp_money);//当月提出比例
					$commission=number_format($temp_money*$rate, 2, '.', '');//当月总佣金提成
					$dataList[]=array('agent_id'=>$value_agent,'date'=>date('Y')."年".date('m',strtotime(" -1 month "))."月",'consume'=>$temp_money,'commission'=>$commission,'rate'=>($rate*100).'%','created'=>time(),'modfied'=>time(),'deleted'=>1,'code_key'=>date('Ym',strtotime(" -1 month ")));
				}
				//开启事务
				$model = new Model();
				$model->startTrans();
				//批量写入
				$add_result = $reportModel->addAll($dataList);
				//修改用户月报表状态
				$save_temp = true;
				foreach ($report_list as $k_report => $v_report) {
					$data_report['user_report_id']=$v_report['user_report_id'];
					$data_report['status']=2;
					$save_result = D("UserReport")->save($data_report);
					if($save_result===false){
						$save_temp=false;
					}
				}
				if($add_result&&$save_temp){
					$model->commit();
					echo "<script>alert('统计完成！');</script>";
				}else{
					$model->rollback();
					echo "<script>alert('统计失败！');</script>";
				}
			}else{
				echo "<script>alert('上个月已统计过！');</script>";
			}
			$this->get_index_data();
			$this->display("index");
		
	}
	
	//结算代理佣金到余额
	function agent_to_money(){
		$reportModel = D("Report");
		$where['agent_id']=array('neq',0);
		$where['status']=1;
		$list = $reportModel->where($where)->select();
// 		dump($list);
// 		die();
		if($list){
			//开启事务
			$model = new Model();
			$model->startTrans();
			$agentModel = D("Agent");
			$reportModel = D("Report");
			$temp=true;
			foreach ($list as $key => $value) {
				$result_setInc = $agentModel->where('agent_id='.$value['agent_id'])->setInc('money',$value['commission']); // 用户的积分加3
				
				$result_setField = $reportModel->where('agent_id='.$value['agent_id'])->setField('status','2');
				if($result_setInc===false||$result_setField===false){
					$temp=false;
				}
			}
			if($temp){
				$model->commit();
				echo "<script>alert('结算完成！');</script>";
			}else{
				$model->rollback();
				echo "<script>alert('结算失败！');</script>";
			}
		}else {
			echo "<script>alert('暂时没有可以结算的佣金！');</script>";
		}
		
		$this->display("index");
// 		$this->redirect("index");
	}
	
	private function get_index_data(){
		$userReportList = D("UserReport")->field('date')->select();
		$this->userReportDate = get_user_id_from_array($userReportList,'date');
		$reportList = D("Report")->field('date')->select();
		$this->reportDate = get_user_id_from_array($userReportList,'date');
		$where_status['status']=2;
		$reportListStatus = D("Report")->where($where_status)->field('date')->select();
		$this->reportDateStatus = get_user_id_from_array($reportListStatus,'date');
	}
	
	function add() {
		parent::add ();
	}
	function insert() {
		parent::__config ( array (
				'display' => 'index' 
		) );
	}
	function foreverdelete() {
		parent::__config ( array (
				'display' => 'index?table=1' 
		) );
		parent::foreverdelete ();
	}
	function edit() {
		parent::__config ( array (
				'display' => 'index' 
		) );
		$data ['id'] = I ( 'id' );
		$data ['status'] = 1;
		parent::update ( $data );
		// parent::edit ();
	}
	function update() {
		parent::__config ( array (
				'display' => 'index' 
		) );
		$data ['id'] = I ( 'id' );
		$data ['status'] = 0;
		parent::update ( $data );
	}

	function bean() {
		parent::bean ();
	}
	function sort() {
		parent::sort ();
	}
	function excelImport() {
	}
	function excelOutput() {
	}
}